<html>
<body>
<cfif isDefined("FORM.sendit")><br />
	<cfset galaxySize = FORM.galaxySize />
	<cfset fortyPercentRule = galaxySize*.4 />
    <cfset theChanceRule = fortyPercentRule*.5>
    <cfset theSQRRT = #Sqr(galaxySize)# />
	<cfset theINT = #Int(Sqr(galaxySize))# />
    <cfset theGrid = theINT * theINT>
    <cfif galaxySize GT theGrid>
    	<cfset wormHoleCount = galaxySize - theGrid>
    <cfelse>
    	<cfset wormHoleCount = galaxySize * .1>
    </cfif>
    <cfoutput>
    	Size: #galaxySize#<br />
		Possible Planets: #fortyPercentRule#<br />
        Chance Roll: #theChanceRule#<br />
        Square Root of Size: #theSQRRT#<br />
        Grid Size (INT of SQRRT): #theINT# X #theINT#<br />
        Grid Cell Count (Total): #theGrid#<br />
        Wormhole Count: #wormHoleCount#<br />
	</cfoutput>
    <cfset sectorNumber=0>
    <table border="1" cellpadding="5" cellspacing="0">
    <cfloop from="1" to="#theINT#" index="theRow">
    	<tr>
			<cfloop from="1" to="#theINT#" index="theColumn">
            <cfset sectorNumber = sectorNumber+1>
            <td>
				<cfoutput>
                    Sec&nbsp;No:&nbsp;#sectorNumber#<br />
                    Sec:#theRow#,#theColumn#<br />
                    <cfif fortyPercentRule is not 0>
                    	<cfset tempy = Rand("SHA1PRNG")>
					    <cfset theChance = #RandRange(1, fortyPercentRule, "SHA1PRNG")#>
                    <cfelse>
    	                <cfset theChance = 0>
                    </cfif>
					<!--- First Sector --->
                    <cfif theRow is 1 and theColumn is 1>
                    	Has Planet: Yes<br>
	                    Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber+1#)-(#sectorNumber+theINT#)-(#sectorNumber+theINT+1#)
                    <!--- Last Sector of First Row --->
                    <cfelseif theRow is 1 and theColumn is theINT>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
                    	Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber-1#)-(#sectorNumber+theINT-1#)-(#sectorNumber+theINT#)
                    <!--- Cells in the first row - other than the first and last --->
                    <cfelseif theRow is 1>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp(#sectorNumber-1#)-(#sectorNumber+1#)-(#sectorNumber+theINT-1#)-(#sectorNumber+theINT#)-(#sectorNumber+theINT+1#)
					<!--- First cell of Last Row --->
					<cfelseif theRow is theINT and theColumn is 1>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber+1#)-(#sectorNumber-theINT#)-(#sectorNumber-theINT+1#)
					<!--- Last cell of Last Row --->
					<cfelseif theRow is theINT and theColumn is theINT>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber-1#)-(#sectorNumber-theINT#)-(#sectorNumber-theINT-1#)
                    <!--- other cells in the last row --->
					<cfelseif theRow is theINT>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp(#sectorNumber-1#)-(#sectorNumber+1#)-(#sectorNumber-theINT-1#)-(#sectorNumber-theINT#)-(#sectorNumber-theINT+1#)
                    <!--- First cell of any row other than 1 and the max row count --->
					<cfelseif theRow is not 1 and theColumn is 1>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber-theINT#)-(#sectorNumber-theINT+1#)-(#sectorNumber+1#)-(#sectorNumber+theINT#)-(#sectorNumber+theINT+1#)
                    <!--- Last cell of any row other than 1 and the max row count --->
					<cfelseif theRow is not 1 and theColumn is theINT>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber-theINT#)-(#sectorNumber-theINT-1#)-(#sectorNumber-1#)-(#sectorNumber+theINT#)-(#sectorNumber+theINT-1#)
					<!--- All middle cells --->
                    <cfelse>
						<cfif theChance GTE theChanceRule>
                        	Has Planet: Yes <cfset fortyPercentRule = fortyPercentRule -1> <cfoutput>#theChance#</cfoutput><br />
                        <cfelse>
                        	Has Planet: No <cfoutput>#theChance#</cfoutput><br />
                        </cfif>                    	
	                    Adjoining&nbsp;Sectors:&nbsp;(#sectorNumber-theINT-1#)-(#sectorNumber-theINT#)-(#sectorNumber-theINT+1#)-(#sectorNumber-1#)-(#sectorNumber+1#)-(#sectorNumber+theINT-1#)-(#sectorNumber+theINT#)-(#sectorNumber+theINT+1#)
                    </cfif>
				</cfoutput>
            </td>
			</cfloop>
        </tr>
    </cfloop>
    </table>
    <br />
    Remaining Planets: <cfoutput>#fortyPercentRule#</cfoutput><br />
<cfelse>
	<cfform action="index.cfm" method="post" name="theTest">
    	<cfinput type="text" name="galaxySize" validate="range" range="250,25000">
        <cfinput type="submit" name="sendit">
    </cfform>
</cfif>
</body>
</html>